<?php
/**
 * 商品
 */

namespace app\api\controller;


use app\api\model\Collection;
use app\api\model\GoodsComment;
use think\Request;
use app\api\model\Goods as GoodsModel;
class Goods extends Common
{
   public function getGoodsInfo(Request $request)
   {
       $user_id = $this->checkLoginUser();

       //$user_id=74;
       $goods_id = $request->param('goods_id');
       $info = GoodsModel::getGoodsInfo($goods_id);
       if(empty($info))
       {
           to_json_encode(400,'error',['status'=>false]);
       }
       $info = GoodsModel::handleGoodsInfo($info,$user_id);
       $info['points']=GoodsModel::getGoodsPoints($goods_id);
       $info['is_user_collect']=$user_id?Collection::isUserCollection($user_id,$goods_id):false;
       to_json_encode(200,'success',$info);

   }
    public function postAddComment(Request $request)
    {
        $user_id = $this->checkLoginUser(true);
       //$user_id=$request->post('user_id');
        $order_id=$request->post('order_id');
        $goods_id=$request->post('goods_id');
        $content = $request->post('content','','htmlspecialchars');
        $images = $request->post('images');
        $stars =$request->post('stars');

        if(empty($order_id) || empty($goods_id) || empty($content))
        {
            to_json_encode(400,'内容不能为空',['status'=>false]);

        }
        $stars =$stars==0?5:$stars;
       
        $image_names= [];
        if($images)
        {
            //处理上传图片
            foreach ($images as $key=>$val)
            {
                $image_names[]=get_image_save_path($val);

            }
        }
        
        if(GoodsComment::addComment($user_id,$content,$stars,$image_names,$order_id,$goods_id)){
            GoodsModel::incrGoodsCommentNum($goods_id);
            to_json_encode(200,'success',['status'=>true]);

        }
        to_json_encode(400,'fail',['status'=>false]);

    }
    //上传 评论图片
    public function postUploadCommentPic(Request $request)
    {
        $images=$request->file();
        $image_names= [];
        if($images)
        {
            //处理上传图片
            foreach ($images as $key=>$val)
            {
                $save_name = handle_upload_image($val,GoodsComment::COMMENT_IMAGE_SAVE_PATH);
                if($save_name)
                {
                    $image_names[]=get_image_url(GoodsComment::COMMENT_IMAGE_SAVE_PATH,$save_name);
                }else{
                    file_put_contents('goods_comment_image_fail.log',date('Y-m-d H:i:s'.PHP_EOL),FILE_APPEND);
                }
            }
        }

        $res = ['result'=>0,'message'=>'','data'=>['url'=>$image_names[0]]];
        die(json_encode($res));
        //to_json_encode(200,'success',$image_names);
    }
    public function getCommentList(Request $request)
    {
        $page = $request->param('page',1,'intval');
        $goods_id = $request->param('goods_id',0,'intval');

        if(empty($goods_id))
        {
            to_json_encode(400,'参数错误',['status'=>false]);

        }

        to_json_encode(200,'success',GoodsComment::handleCommentList(GoodsComment::getComment($goods_id,$page)));

    }
    public function postDeleteComment(Request $request)
    {
        $user_id = $this->checkLoginUser(true);
        $comment_id = $request->post('comment_id',0,'intval');
        if(empty($comment_id))
        {
            to_json_encode(400,'参数错误',['status'=>false]);

        }
        $info = GoodsComment::getCommentInfo($comment_id);
        if(empty($info) || $info['user_id'] != $user_id)
        {
            to_json_encode(400,'没有该评价',['status'=>false]);

        }
        if(GoodsComment::deleteComment($comment_id,$user_id))
        {
            GoodsModel::descGoodsCommentNum($info['goods_id']);
            to_json_encode(200,'success',['status'=>true]);
        }
        to_json_encode(400,'fail',['status'=>false]);

    }
}